package zl_service

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/goravel/framework/support/carbon"
	"io/ioutil"
	"net/http"
)

const (
	QUERY_URL = "/zl.open.api.oms.shop.queryorderdetail.v5/1.0.0/action"
)

type QueryParam struct {
	AppKey     string `json:"appkey"`
	Signtime   string `json:"signtime"`
	Ticketcode string `json:"ticketcode" comment:"销售单流水号"`
}

// 查询订单详情
type QueryOrderService struct {
}

func (q *QueryOrderService) QueryOrder(ticketcode string) error {
	url := BASE_URL + QUERY_URL
	client := &http.Client{}

	orderParams := QueryParam{
		AppKey:     APP_KEY,
		Signtime:   carbon.NewDateTime(carbon.Now()).Format("Y-m-d H:i:s"),
		Ticketcode: ticketcode,
	}

	bodyBytes, _ := json.Marshal(orderParams)
	stringToSign := fmt.Sprintf("%s&appsecret=%s", string(bodyBytes), APP_SECRET)
	upperSign := ComputeMD5AndToUpper(stringToSign)
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(bodyBytes))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Request-Sign", upperSign)
	body, err := client.Do(req)
	if err != nil {
		return err
	}
	//将body
	res, err := ioutil.ReadAll(body.Body)
	fmt.Println(string(res))
	return nil
}
